package com.eascs.admin.o2o.bill;

import java.util.Date;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.bill.entity.BillCodeEntity;
import com.eascs.common.util.UuidUtil;
@Repository
public class BillCode extends BaseO2oDao{
	
	public String getBillCode(String code,String type) {
		String bill=StringUtils.getFourBitYearMonth(new Date());
		String sql="select * from sequence_no n where n.s_date=? order by s_no desc";
		BillCodeEntity billCodeEntity=this.findEntity(sql, BillCodeEntity.class, bill);		
		BillCodeEntity bi=new BillCodeEntity();
		if (billCodeEntity==null) {
			String being="0001";
			bi.setId(UuidUtil.generateUuidWithoutLine());
			bi.setSdate(bill);
			bi.setSno(being);
			this.insert(bi);
			return code+"_"+type+"_"+bill+"_"+being;
		}else{
			Integer item=Integer.valueOf(billCodeEntity.getSno())+1;
			String being=StringUtils.formatd(item, 4);
			bi.setId(UuidUtil.generateUuidWithoutLine());
			bi.setSdate(bill);
			bi.setSno(being);
			this.insert(bi);
			return code+"_"+type+"_"+billCodeEntity.getSdate()+"_"+being;
		}
	}
}
